/*
 * Copyright (c) 2011-2013, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: mHelperApplicationFunctionWrapper.c 6161 2013-12-18 17:08:28Z geisinger $
 */

/**
 * \file
 *         Function wrapper - a generic abstraction for one executable function
 *              scheduled by the execution manager.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "../include/mHelperApplicationFunction.h"

#include "../include/mHelperApplicationFunctionWrapper.h"

#include "xme/core/executionManager/include/executionManagerDataStructures.h"
#include "xme/core/executionManager/include/executionManagerWrapperInterface.h"

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
void
test_mHelperApplication_mHelperApplicationFunctionWrapper_execute
(
    void* userFunctionDescRaw
)
{
    xme_core_exec_functionDescriptor_t* funDesc;
    void* functionParameter = NULL;

    funDesc = (xme_core_exec_functionDescriptor_t*)userFunctionDescRaw;
    functionParameter = NULL;

    xme_core_exec_dispatcher_initializeTask
    (
        funDesc->componentId,
        funDesc->functionId
    );

    if (NULL != funDesc->init)
    {
        xme_status_t status = XME_STATUS_INTERNAL_ERROR;

        status = funDesc->init(funDesc->taskArgs);
        if (XME_STATUS_SUCCESS != status)
        {
            /* TODO: design a startup and signaling mechanism for execution manager / or some other manager */
            XME_LOG(XME_LOG_FATAL,
                    "Function [%d|%d] initialization returned an error code %d\n",
                    funDesc->componentId,
                    funDesc->functionId,
                    status);
            funDesc->state = XME_CORE_EXEC_FUNCTION_STATE_TERMINATED;
        }
    }

    for (;;)
    {
        xme_core_exec_dispatcher_waitForStart
        (
            funDesc->componentId,
            funDesc->functionId,
            &functionParameter
        );

        if (XME_CORE_EXEC_FUNCTION_STATE_TERMINATED == funDesc->state)
        {
            xme_core_exec_dispatcher_executionCompleted
            (
                funDesc->componentId,
                funDesc->functionId
            );
            
            return;
        }

        if (XME_CORE_EXEC_FUNCTION_STATE_PAUSED != funDesc->state)
        {
            test_mHelperApplication_mHelperApplicationFunction_step(userFunctionDescRaw);
        }

        xme_core_exec_dispatcher_executionCompleted
        (
            funDesc->componentId,
            funDesc->functionId
        );
    }

    if (NULL != funDesc->fini)
    {
        funDesc->fini(funDesc->taskArgs);
    }
}
